﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using ContractManager.BLL.Business;
using ContractManager.Common.Class;
using ContractManager.Common.FormBase;
using DevExpress.XtraLayout.Utils;

namespace ContractManager.Lists.Contracts
{
    public partial class FrmContractFind : XtraFormBase
    {
        #region "Private Variable"

        private readonly BackgroundWorker _loadworker = new BackgroundWorker();
        public int ConId;
        public string ConName;
        public string ConNo;
        public bool IsFind;

        private DataTable _dtContracts;

        #endregion

        #region "Construction"

        public FrmContractFind()
        {
            InitializeComponent();
            InitializeControls();
            ClsVariable.DoubleBuffer(this);
            _loadworker.DoWork += loadworker_DoWork;
            _loadworker.RunWorkerCompleted += loadworker_RunWorkerCompleted;
        }

        #endregion

        #region "Private Method"

        private void InitializeControls()
        {
            grdContract.DataSource = bdsMain;
            grdContract.UseEmbeddedNavigator = true;
            grvContract.OptionsBehavior.Editable = false;
        }

        private void BlankForm()
        {
            ResetControls();
        }

        public void GetData()
        {
            if (!_loadworker.IsBusy)
            {
                _loadworker.RunWorkerAsync();
                ClsVariable.SetWaitDialogCaption(string.Format("Nạp danh sách {0}...", Text));
            }
        }

        private void LoadLookupData()
        {
            txtConTypId.Properties.DataSource = ClsContractType.SelectAll();
            txtConTypId.Properties.DisplayMember = "ConTypName";
            txtConTypId.Properties.ValueMember = "ConTypId";

            txtConPartA.Properties.DataSource = ClsInvestors.SelectAll();
            txtConPartA.Properties.DisplayMember = "InvName";
            txtConPartA.Properties.ValueMember = "InvID";
        }

        private void DoSelect()
        {
            int focusedRowHandle = grvContract.FocusedRowHandle;
            if (focusedRowHandle >= 0)
            {
                ConId = Convert.ToInt32(grvContract.GetRowCellValue(focusedRowHandle, "ConId"));
                ConNo = String.Format("{0}", grvContract.GetRowCellValue(focusedRowHandle, "ConNo"));
                ConName = String.Format("{0}", grvContract.GetRowCellValue(focusedRowHandle, "ConName"));
                Close();
            }
        }

        #endregion

        #region "Background Worker"

        private void loadworker_DoWork(object sender, DoWorkEventArgs e)
        {
            int invId = txtConPartA.EditValue == null ? 0 : Convert.ToInt32(txtConPartA.EditValue);
            int conTypId = txtConTypId.EditValue == null ? 0 : Convert.ToInt32(txtConTypId.EditValue);
            DateTime fromDate = txtFromDate.EditValue == null ? txtFromDate.Properties.MinValue : txtFromDate.DateTime;
            DateTime toDate = txtToDate.EditValue == null ? txtToDate.Properties.MaxValue : txtToDate.DateTime;

            try
            {
                _dtContracts = ClsContracts.Find(113, VoucherCate, fromDate, toDate,
                                                 txtConNo.Text, txtConName.Text, txtConDescription.Text,
                                                 txtAmountFrom.Value, txtAmountTo.Value, invId, conTypId);
                e.Result = _dtContracts;
            }
            catch (Exception)
            {
                e.Cancel = true;
            }
        }

        private void loadworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Action a = () => { bdsMain.DataSource = e.Result; };
            grdContract.Invoke(a);
            ClsVariable.SetWaitDialogCaption(string.Format("Danh sách {0} đã được nạp", Text));
        }

        #endregion

        #region "Form Event"

        private void FrmContractFind_Load(object sender, EventArgs e)
        {
            BlankForm();
            LoadLookupData();
            layoutControlItem12.Visibility = IsFind ? LayoutVisibility.Always : LayoutVisibility.Never;

            //if (!String.IsNullOrEmpty(FormXtraLayoutTemplate))
            //{
            //    string fileLayout = ClsVariable.GetRestoreLayoutFullPath(FormXtraLayoutTemplate);
            //    layoutControl.RestoreLayoutFromXml(fileLayout);
            //}
            //if (!String.IsNullOrEmpty(FormXtraGridLayoutTemplate))
            //{
            //    string filePath = ClsVariable.GetRestoreLayoutFullPath(FormXtraGridLayoutTemplate);
            //    grvContract.RestoreLayoutFromXml(filePath, OptionsLayoutBase.FullLayout);
            //}
        }

        #endregion

        #region "Button Event"

        private void btnSearch_Click(object sender, EventArgs e)
        {
            GetData();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            DoSelect();
        }

        #endregion

        #region "XtraGrid Event"
        private void grvContract_DoubleClick(object sender, EventArgs e)
        {
            if (grvContract.IsDataRow(grvContract.FocusedRowHandle))
            {
                DoSelect();
            }
        }

        private void grdContract_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (grvContract.IsGroupRow(grvContract.FocusedRowHandle)) return;
            if (e.KeyCode == Keys.Space)
            {
                if (grvContract.FocusedRowHandle < grvContract.DataRowCount - 1)
                {
                    grvContract.FocusedRowHandle += 1;
                    e.Handled = true;
                }
            }
            if (e.KeyCode == Keys.Back)
            {
                if (grvContract.FocusedRowHandle > 0)
                {
                    grvContract.FocusedRowHandle -= 1;
                    e.Handled = true;
                }
            }
            if (e.KeyCode == Keys.Enter)
            {
                DoSelect();
            }
        }

        #endregion

    }
}